package com.daxia.ogmall.dao;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.daxia.ogmall.common.DecorateRequirementStatus;
import com.daxia.ogmall.daobase.GenericDAOHibernate;
import com.daxia.ogmall.dto.DecorateRequirementDTO;
import com.daxia.ogmall.model.DecorateRequirement;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.DateUtils;

@Repository
public class DecorateRequirementDAO extends GenericDAOHibernate<DecorateRequirement> {

    public List<DecorateRequirement> find(DecorateRequirementDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From DecorateRequirement n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getHouseType() != null) {
        	if (dto.getHouseType().getId() != null) {
	            hql.append(" and n.houseType.id = ? ");
	            paras.add(dto.getHouseType().getId());
        	}
        }			
        if (dto.getHouseStyle() != null) {
        	if (dto.getHouseStyle().getId() != null) {
	            hql.append(" and n.houseStyle.id = ? ");
	            paras.add(dto.getHouseStyle().getId());
        	}
        }
        if (dto.getStatus() != null) {
        	hql.append(" and n.status = ? ");
        	paras.add(dto.getStatus());
        }
        
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
        		hql.append(" and n.user.id = ? ");
        		paras.add(dto.getUser().getId());
        	}
        }
        if (dto.getDesigner() != null) {
        	if (dto.getDesigner().getUser() != null) {
        		if (dto.getDesigner().getUser().getId() != null) {
        			hql.append(" and n.designer.user.id  = ? ");
        			paras.add(dto.getDesigner().getUser().getId());
        		}
        	}
        }
        if (dto.getCityId() != null) {
        	hql.append(" and n.user.city.id = ? ");
        	paras.add(dto.getCityId());
        }
        
        if (StringUtils.isNotBlank(dto.getSort())) {
        	hql.append(dto.getSort());
        }
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public DecorateRequirement findOne(DecorateRequirementDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<DecorateRequirement> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

	public Integer updateByIdAndDesignerId(Long id, Long designerId) {
		StringBuilder hql = new StringBuilder("UPDATE decoraterequirement  SET designer_id = ?  , STATUS = 1 WHERE id = ?  AND STATUS = 0");
	    List<Object> paras = new ArrayList<Object>();
	    paras.add(designerId);
	    paras.add(id);
		return super.executeSQLUpdate(hql.toString(), paras.toArray());
	}

	public BigInteger countAllOfADay() {
		StringBuilder hql = new StringBuilder("SELECT COUNT(*) FROM decoraterequirement WHERE createTime <= ? ");
		List<Object> paras = new ArrayList<Object>();
		paras.add(DateUtils.getEndOfADay(new Date()));
		return (BigInteger)super.executeSQLQueryUniqueResult(hql.toString(), paras);
	}

	public BigInteger countSuccedOfADay() {
		StringBuilder hql = new StringBuilder("SELECT COUNT(*) FROM decoraterequirement WHERE status = ? and  createTime <= ? and createTime >= ? ");
		List<Object> paras = new ArrayList<Object>();
		paras.add(DecorateRequirementStatus.Done.getValue());
		paras.add(DateUtils.getEndOfADay(new Date()));
		paras.add(DateUtils.getBeginningOfADay(new Date()));
		return (BigInteger)super.executeSQLQueryUniqueResult(hql.toString(), paras);
	}
}
